package minh.bac.wcbrazil2014.homnay;

import java.util.ArrayList;
import java.util.Calendar;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.adapter.ListViewAdapter;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import minh.bac.wcbrazil2014.object.VongDau;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

public class ListViewHomnayFM extends Fragment {

	ListView listView;
	ArrayList<MatchScheduleObj> arraMatchScheduleObjs;
	private ArrayList<VongBangObj> mVongbangList;
	private ArrayList<VongDau> mVongDauList;
	private int mTypeShow;
	ListViewAdapter adapter;
	private MySQLiteManager mMysLiteManager;
	ArrayList<String> arrayDay;
	DisplayImageOptions displayImageOptions;

	public static ListViewHomnayFM newInstance() {
		ListViewHomnayFM fm = new ListViewHomnayFM();
		return fm;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.listview_homnay, container,
				false);
		listView = (ListView) rootView.findViewById(R.id.listview_homnay);
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH) + 1;
		int date = calendar.get(Calendar.DATE);
		String d = "", m = "";
		if (month < 10) {
			m = "0" + month;
		} else
			m = month + "";
		if (date < 10) {
			d = "0" + date;
		} else
			d = date + "";
		mVongbangList = new ArrayList<>();
		mVongDauList = new ArrayList<>();
		arraMatchScheduleObjs = new ArrayList<>();
		mMysLiteManager = new MySQLiteManager(getActivity());
		String time = d + "/" + m + "/2014";
		Log.d("HOMNAY", "time " + time);
		mVongbangList = mMysLiteManager.getVongBang();
		mVongDauList = mMysLiteManager.getVongDau();
//		mVongDauList = mMysLiteManager.getVong18();
		displayImageOptions = new DisplayImageOptions.Builder()
				.cacheInMemory(false).cacheOnDisc(true)
				.resetViewBeforeLoading(false)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.showImageForEmptyUri(R.drawable.ic_emblem_club_placeholder)
				.showImageOnFail(R.drawable.ic_emblem_club_placeholder)
				.imageScaleType(ImageScaleType.EXACTLY).build();
		// /
		// /
		// /
		for (int i = 0; i < mVongbangList.size(); i++) {
			if (mVongbangList.get(i).getNgay().indexOf(time) != -1) {
				arraMatchScheduleObjs.add(mVongbangList.get(i));
			}
		}
		if (arraMatchScheduleObjs != null && arraMatchScheduleObjs.size() != 0) {
			
			mTypeShow = WCTAG.TYPE_SHOW_VONGBANG;
			adapter = new ListViewAdapter(getActivity(),
					R.layout.itemchild_listview, arraMatchScheduleObjs,
					displayImageOptions, mTypeShow);
			listView.setAdapter(adapter);
		} else {
			mTypeShow = 29;
			//
			for (int i = 0; i < mVongDauList.size(); i++) {
				if (mVongDauList.get(i).getNgay().indexOf(time) != -1) {
					arraMatchScheduleObjs.add(mVongDauList.get(i));
				}
			}
			if (arraMatchScheduleObjs != null
					|| arraMatchScheduleObjs.size() != 0) {
				mTypeShow = 10;
				adapter = new ListViewAdapter(getActivity(),
						R.layout.itemchild_listview, arraMatchScheduleObjs,
						displayImageOptions, mTypeShow);
				listView.setAdapter(adapter);
			}
		}
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				HomNayItemListViewFM fragment = HomNayItemListViewFM
						.newInstance(mTypeShow);
				MatchScheduleObj matchScheduleObj = arraMatchScheduleObjs
						.get(position);
				fragment.setMatchScheduleObj(matchScheduleObj);
				HomNayFragment.switchFragment(R.id.container_homnay, fragment,
						true, null);
			}
		});
		Log.d("HOMNAY", "run homnay");
		return rootView;
	}

}
